#include "main.h"
#include "drv_spi.h"
#include "app_can.h"
#include "app_input.h"
#include "app_output.h"
#include "74hc595.h"
#include "app_can.h"
#include "drv_pi.h"
#include "drv_usart2.h"
#include "hem_port.h"
#include "I2C1.h"
#include "app.h"
#include "flash.h"
#include "Lib.h"
#include "app_input.h"
#include "fmc_operation.h"
#include "hem_common.h"
#include "user_define.h"
#include "app_can.h"

#include "I2C1.h"
#include "flash.h"
#include "drv_dac.h"
#include "drv_pi.h"
#include "drv_spi.h"
#include "drv_usart2.h"

#include "wireless.h"
#include "Lib.h"
#include "para.h"

#include "hem_port.h"
#include "drv_can_lib.h"
uint8_t code_dateStr[] = __DATE__;
uint8_t code_timeStr[] = __TIME__;
uint32_t g_year   = YEAR;
uint32_t g_moth   = MONTH+1;
uint32_t g_day    = DAY;
uint32_t g_hour   = HOUR;  
uint32_t g_minute = MINUTE;
uint32_t g_sec    = SECOND;
/**
 * @brief 硬件模块初始化
 */
void hem_hardware_module_init(void) 
{
  hem_adc_init();
  // PWM设置
  PWM_Init(g_config.pwm_freq[0], g_config.pwm_freq[1], g_config.pwm_freq[2],
           g_config.pwm_freq[3], g_config.pwm_freq[4], g_config.pwm_freq[5]);

  drv_pi_init();
}
/**
 * @brief HEM初始化部分
 */
void board_init(void) 
{
  // 系统时钟初始化
 // extern void systick_config(void);
  systick_config();

  nvic_priority_group_set(NVIC_PRIGROUP_PRE2_SUB2);

  // 模块时钟初始化
  hem_rcu_init();

  // 模块引脚初始化
  hem_pin_init();

  // hem硬件模块初始化
  hem_hardware_module_init();
}
/**
 * @brief 参数初始化
 */
void param_init(hem_config_t *config)
{
  uint8_t i = 0;
  
#if HEM_AI_NUM > 0
  for (i = 0; i < HEM_IN_PORT_NUM; i++) 
  {
    config->in_mode[i] = IN_MODE_20MA;
  }
#endif
#if HEM_PWM_NUM > 0
	for(i = 0;i < HEM_PWM_FREQ_NUM;i++)
	{
		config->pwm_freq[i] = 200;           //PWM频率
	}
#endif
  
#if HEM_PWMI_NUM > 0
  config->pwmi_kp100 = 112; // 112
  config->pwmi_ki100 = 48;	// 48
  config->pwmi_kd100 = 16;	// 16
#endif
}
void node_config_init(hem_config_t *config) 
{
  int32_t rc = hem_config_load(&g_config);
  if (rc == 0) // succeed
  {
    if(memcmp(&g_config,config,sizeof(hem_config_t)) == 0)
    {
      return;
    }
  }
  memcpy(&g_config, config, sizeof(hem_config_t));
  g_config.config_ok = FLASH_CONFIG_OK;
  hem_config_save(&g_config);
}
/**
 * @brief 参数初始化,放在硬件初始化之前
 */
void Main_SwInit(void)
{
  // read node config para from flash
  hem_config_t default_config;
  default_config.config_ok = FLASH_CONFIG_OK;
  default_config.baudrate[0] = 250;
  default_config.baudrate[1] = 250;
  
	
	default_config.in_mode[0] = IN_MODE_20MA;
	
  node_config_init(&default_config);
  load_pwmi_calibration_param();

  /*CAN0 ID 注册*/
  can0_register_std_list_id(DOWNLOAD_CMD_ID);   /*勿删除，下载程序CANID*/
  can0_register_std_list_id(HEK1016_RX_ID_01);

  /*CAN1 ID 注册*/
  can1_register_std_list_id(DOWNLOAD_CMD_ID);   /*勿删除，下载程序CANID*/

}
/**
 * @brief 硬件初始化
 */
void Main_HwInit(void)
{
  SCB->VTOR = FLASH_BASE | 0x00008000;  
  board_init();
  app_input_init();
  Main_PF15_Config();
  app_can_init();
  drv_dac_init();   
  I2C_Port_Init();
  Main_pca9555_init();
  rs485_init(19200,parity_NONE,stop_bit_1,data_bit_8);

  SPI_Init();
  wireless_select_out(eNss,1);
  wireless_init();
}

int32_t main(void) 
{
  Main_SwInit();
  in_select();
  Main_HwInit();
  
  while (1) 
  {
		in_select();
    port_configure_run();
  	drv_System_Reset(); 

    /*******user code start*******/
    app_run();
    /*******user code end*******/
  }
}

/**
 * @brief 500us中断函数，每500us中断一次
 */
volatile uint32_t tick_cntr_500us = 0;
void task_500us(void) 
{
  tick_cntr_500us++;
	adc_multiplexing();//勿删除
  App_tick(tick_cntr_500us);
  
  if (tick_cntr_500us%TIME_OUT_1MS == 0) 
  {
    output_update();
  } 
  else 
  { 
    input_update();
    Can0_Tx_Fifo();
    Can1_Tx_Fifo();
  }
}


